<?php

/* explain:		authority class
 * Project:     marsems
 * File:        Authority.php
 *
 * @ link 		http://www.marsems.com/lib
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming	
 * @ author 	ivan.ling
 * @ version 1.0
 * 
 */
include_once("Common.php");
include_once("Logs.php");
class Authority extends Common
{
	private $conHandle;
	public  $strLanguage;

	private $iUserID;
	
	private $objLog;
	
	function Authority($strLanguage, $uid)
	{
		parent::Common();
		$this->conHandle = $this->getConnection("system");
		$this->strLanguage = $strLanguage;

		$this->objLog 		= new Logs($usrid);
		$this->iUserID 		= $usrid;
	}
		
	public function setLan($strLan)
	{
		$this->strLanguage = $strLan;
	}
	
	public function getMenuFromUserID($userid)
	{
		if($this->strLanguage == "zh-CN"){
			$strGname = "namecn";
			$strMname = "namecn";
		}else{
			$strGname = "name";
			$strMname = "name";
		}
			
		if($userid == 1)
			$sql = "SELECT g.id as gid, m.id as mid, g.$strGname as gname, m.$strMname as mname, m.url FROM menu_groups g, menu m WHERE g.id = m.groupid";
		else
			$sql = "SELECT g.id as gid, m.id as mid, g.$strGname as gname, m.$strMname as mname, m.url FROM authority a, menu_groups g, menu m WHERE g.id = m.groupid AND m.id = a.menuid AND a.userid = '$userid'";
				
		$rs = mysql_query($sql, $this->conHandle);
		
		$arrReturn = array();
		if($rs){
			while($arrData = mysql_fetch_array($rs)){
				$arrReturn[$arrData['gid']][] = $arrData;
			}
		}
		return $arrReturn;
	}
	
	public function getMenuFromGroupID($groupid)
	{
		if($this->strLanguage == "zh-CN"){
			$strGname = "namecn";
			$strMname = "namecn";
		}else{
			$strGname = "name";
			$strMname = "name";
		}
			
		if($groupid == 1)
			$sql = "SELECT g.id as gid, m.id as mid, g.$strGname as gname, m.$strMname as mname, m.url FROM menu_groups g, menu m WHERE g.id = m.groupid ORDER BY g.orders, m.orders";
		else
			$sql = "SELECT g.id as gid, m.id as mid, g.$strGname as gname, m.$strMname as mname, m.url FROM group_authority a, menu_groups g, menu m WHERE g.id = m.groupid AND m.id = a.menuid AND a.groupid = '$groupid' ORDER BY g.orders, m.orders";
				
		$rs = mysql_query($sql, $this->conHandle) or die (mysql_error($this->conHandle).$sql);
		
		$arrReturn = array();
		if($rs){
			while($arrData = mysql_fetch_array($rs)){
				$arrReturn[$arrData['gid']][] = $arrData;
			}
		}
		return $arrReturn;
	}
	
	
	
	/* permission list
	 *
	 * @ param  int 	userid   	
	 * @ param  return 	array		data array
	 */
	public function listPermission($userid)
	{
		if(empty($userid))
			return false;
		
		$sql = "SELECT m.id as mid, m.name as name, m.remark as remark, a.operate as operate FROM menu m left join authority a on (m.id = a.id AND a.userid = $userid)";
		$rs  = mysql_query($sql, $this->conHandle);
	
		if($rs){
			
			while(is_array($arrcache = mysql_fetch_array($rs))){
				$arrback[] = $arrcache;
			}
			return $arrback;
		}else
 			return false;
	}
	
	/* permission list
	 *
	 * @ param  int 	userid   	 
	 * @ param  return 	array		data array
	 */
	public function listGroupPermission($groupid)
	{
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
		
		if($this->strLanguage == "zh-CN"){
			$strMname = "namecn";
		}else{
			$strMname = "name";
		}
		
		$sql = "SELECT m.id as mid, m.$strMname as name, m.remark as remark, a.menuid as authMenuID, a.operate as operate FROM menu m left join group_authority a on (m.id = a.menuid AND a.groupid = $groupid)";
		$rs  = mysql_query($sql, $this->conHandle);

		if($rs){
			
			while(is_array($arrcache = mysql_fetch_array($rs))){
				$arrback[] = $arrcache;
			}
			return $arrback;
		}else
 			return false;
	}
	
	public function delAuthority($userid, $arrPopedom = array()){
		if($this->isUnallowableDELETE($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "DELETE FROM authority WHERE userid = $userid'";
		//$rs = mysql_query($sql, $this->conHandle);
		echo $sql.'<br>';
		$rs = true;
		return $rs ? true: false;
	}
	
	public function addAuthority($userid, $menuid, $operate, $arrPopedom = array())
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "INSERT INTO authority SET userid = '$userid', menuid='$menuid', operate='$operate'";
		$rs = mysql_query($sql, $this->conHandle);  
		
		$rs = true;
		return $rs ? true: false;
	}

	public function addAuthTransaction($userid, $arrOperateCache, $arrPopedom = array())
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = 'set session autocommit=false';
		$rs = mysql_query($sql, $this->conHandle) or die (mysql_error());
		
		$sql = 'start transaction';
		$rs = mysql_query($sql, $this->conHandle) or die (mysql_error()); 
		
		$sql = "DELETE FROM authority WHERE userid = '$userid'";
		
		$rs = mysql_query($sql, $this->conHandle);
		
		foreach ($arrOperateCache as $menuid => $arrOperation){
			$sql = "INSERT INTO authority SET userid = '$userid', menuid='$menuid', operate='" . implode(',', $arrOperation) . "'";
			$rs = mysql_query($sql, $this->conHandle);
			if(!$rs){
				$sql = 'rollback';
				$rs = mysql_query($sql, $this->conHandle);
				break;
			}
		}
		
		$sql = 'commit';
		$rs = mysql_query($sql, $this->conHandle) or die (mysql_error()); 
		if(!$rs){
				$sql = 'rollback';
				$rs = mysql_query($sql, $this->conHandle);
		}
		
		$sql = 'set session autocommit=true';
		$rs = mysql_query($sql, $this->conHandle) or die (mysql_error()); 
		
		return $rs ? true: false;
	}
	
	public function addGroupAuthTransaction($arrPopedom = array(), $groupid, $arrOperateCache)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = 'set session autocommit=false';
		$rs = mysql_query($sql, $this->conHandle) or die (mysql_error());
		
		$sql = 'start transaction';
		$rs = mysql_query($sql, $this->conHandle) or die (mysql_error()); 
		
		$sql = "DELETE FROM group_authority WHERE groupid = '$groupid'";
		
		$rs = mysql_query($sql, $this->conHandle);
		
		foreach ($arrOperateCache as $menuid => $arrOperation){
			$sql = "INSERT INTO group_authority SET groupid = '$groupid', menuid='$menuid', operate='" . implode(',', $arrOperation) . "'";
			$rs = mysql_query($sql, $this->conHandle);
			if(!$rs){
				$sql = 'rollback';
				$rs = mysql_query($sql, $this->conHandle);
				break;
			}
		}
		
		$sql = 'commit';
		$rs = mysql_query($sql, $this->conHandle) or die (mysql_error()); 
		if(!$rs){
				$sql = 'rollback';
				$rs = mysql_query($sql, $this->conHandle);
		}
		
		$sql = 'set session autocommit=true';
		$rs = mysql_query($sql, $this->conHandle) or die (mysql_error()); 
		
		return $rs ? true: false;
	}
}
?>
